# Contibutor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

# Use the right mkspecs file
if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
  _platform=win32-clang-g++
else
  _platform=win32-g++
fi

_realname=qt5-base
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
_qtver=5.15.10
pkgver=${_qtver}+kde+r129
pkgrel=3
_commit=e8d9e68d25f4bf305f8b3ca5d678594ee4681baa
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
pkgdesc="A cross-platform application and UI framework (mingw-w64)"
url='https://www.qt.io/'
license=('GPL3' 'LGPL' 'FDL' 'custom')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-dbus"
             "${MINGW_PACKAGE_PREFIX}-double-conversion"
             "${MINGW_PACKAGE_PREFIX}-fontconfig"
             "${MINGW_PACKAGE_PREFIX}-freetype"
             "${MINGW_PACKAGE_PREFIX}-harfbuzz"
             "${MINGW_PACKAGE_PREFIX}-icu"
             "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
             "${MINGW_PACKAGE_PREFIX}-libpng"
             "${MINGW_PACKAGE_PREFIX}-md4c"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-pcre2"
             "${MINGW_PACKAGE_PREFIX}-sqlite3"
             "${MINGW_PACKAGE_PREFIX}-vulkan"
             "${MINGW_PACKAGE_PREFIX}-zlib"
             "${MINGW_PACKAGE_PREFIX}-zstd"
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-firebird2")
             "${MINGW_PACKAGE_PREFIX}-postgresql"
             "git"
             "rsync")
_pkgfn="${_realname/5-/}"
source=(git+https://invent.kde.org/qt/qt/$_pkgfn#commit=$_commit
        0001-adjust-qmake-conf-mingw.patch
        0002-qt-5.8.0-fix-sql-libraries-mingw.patch
        0003-qt-5.8.0-configure-gcc-before-clang.patch
        0004-fix-linking-again-different-static-libs.patch
        0005-qt-5.3.0-win_flex-replace.patch
        0006-qt-5.3.0-win32-g-Enable-static-builds.patch
        0007-qt-5.3.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch
        0008-qt-5.8.0-mingw-dbus-and-pkg-config.patch
        0009-qt-5.8.0-win32-g++-use-qpa-genericunixfontdatabase.patch
        0010-qt-5.8.0-force-using-make-on-msys.patch
        0011-qt-5.8.0-Revert-untangle-use-of-system-vs.-shell-path-list-se.patch
        0012-qt-5.8.0-Revert-fix-quoting-and-path-separators-in-qtPrepareT.patch
        0013-qt-5.3.1-workaround-ansidecl-h-PTR-define-conflict.patch
        0014-qt-5.3.2-dont-add-resource-files-to-qmake-libs.patch
        0015-qt-5.3.2-win32-qt5-static-cmake-link-ws2_32-and--static.patch
        0016-qt-5.4.2-win32-Avoid-platformNativeInterface-segfaults-with-minimal-platform.patch
        0017-qt-5.8.0-win32-do-not-use-fontconfig.patch
        0018-win32-dont-need-flatpack-theme.patch
        0019-qt-5.11-static_icu.patch
        0020-fix-and-enable-iconv-codec.patch
        0021-enable-mingw-schannel-alpn.patch
        0022-qt-5.8.0-cast-errors.patch
        0024-fix-synqt-with-git-build.patch
        0025-enable-avx-support.patch
        0026-fix-mingw-win32-winnt-detection.patch
        0027-qt-5.7.0-qdbus-manager-quit.patch
        0028-work-around-pyside2-brokenness.patch)
sha256sums=('SKIP'
            'a74eec5be1aee1bd3a37af0955b7c5f3d36d54bc64c07e4c2edd5526cdc3cad5'
            'e0a535278057f42e43952405e567c23cc493ef6badeeb3bbce0154953cd545a5'
            'e7252bdc92fa75f067e4beafbf3c844fca3a4bd16d14ac5344256f022a9214d5'
            '02a3b24c02a6797b706582198ab1177648c2653e8870dcdddc5856d6cd629d49'
            '013401295022dbad73df0355124758d337da71b99253461bb84bc7c9320e27e6'
            '61cb036d39a12abc5f98fc2afdc41694471413171e79debd3cd5f464d74dad32'
            'ae7daec747b954c5bd93bf3f37971d173f8fbd277ad11a63b7c88494dad37986'
            '0fb18899437e86bdbea1750a892b18bef76bdf9b85e504afcb65e7f49f421ff0'
            '3a8de25edb2a4643583ad5fa2743475b60fffad931be25314b4067be2413bca4'
            '49eee16b318f1ac8d5ddf45f294b9f88e05c2f7f7ad2808f281961200d61c6cf'
            '9fafed377f435493dd72b1b8f49ca25580f5b3d4100528a5c4ca18d10627dfd7'
            '69f7b27d7c23731e73500e058ec69e7504740d102244f15959977355814de9c5'
            '41acd8702a33a1d2f5e93c7e940c41aaa335aa728bc5562e799ef6077aa00433'
            '274d24f9cd84a7fae5e2c5836c8a3a6108ef2c04c1b8c8e97a11b70975b98ddf'
            'ce0c7e6dcc5af61c7d7db2bbe8921ab3861fd9380be9af62933f8d29cb577d53'
            '58556b73b3fcbddd5658832f012760e3a86637bdd9e6324596b6ecd69981a985'
            '4b2f54549743864e831e57d45269a7d84182d68cbc5f5277daa29167c046ae52'
            '606e6e4888e2efc8e971258ec3cdf43b32c6dac9bdba22ed89ca9571adeb365d'
            'c88c558f3388ab2fa7df57e403324e92abdfbdf72a87c849af337720f52baa8b'
            'c9bdd0ce5f30c6eb940675e45e14179bbab41b743e88cec4679c20dc2d8c9cbf'
            'fec3f368973c004c3fccea4e9d816eb42c02dcb2355714d5c833c8b74b428f11'
            '5397862593005d9af4b218396c4a3975b0e0f039992562ffef4672c0738c4df0'
            '18b3dccd9141773026b2a13a79df42ec9196bc6ad77f5feedcbd304b02fa0e6f'
            'e5d22486bf809d9422103471766f4a11d3090acaf9104cfaf333bad1aa1dcc6c'
            '6ceed12f98254c37fb3b1ab68a84214c922bd2c402fea89fa65638d56e567bbb'
            '03dc899326676cc6e9e6b4e77634ccea3693449bf8627653dc7fdb74378ad264'
            '8dd074ab427ccd439e34f50ed8b03228d71c09a5375050ad5d47064ff6b00d8f')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

pkgver() {
  cd $_pkgfn
  echo "${_qtver}+kde+r"`git rev-list --count v${_qtver}-lts-lgpl..$_commit`
}

prepare() {
  cd $_pkgfn

  apply_patch_with_msg \
    0001-adjust-qmake-conf-mingw.patch \
    0002-qt-5.8.0-fix-sql-libraries-mingw.patch \
    0003-qt-5.8.0-configure-gcc-before-clang.patch \
    0004-fix-linking-again-different-static-libs.patch \
    0005-qt-5.3.0-win_flex-replace.patch \
    0006-qt-5.3.0-win32-g-Enable-static-builds.patch \
    0007-qt-5.3.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch \
    0008-qt-5.8.0-mingw-dbus-and-pkg-config.patch \
    0009-qt-5.8.0-win32-g++-use-qpa-genericunixfontdatabase.patch \
    0010-qt-5.8.0-force-using-make-on-msys.patch \
    0011-qt-5.8.0-Revert-untangle-use-of-system-vs.-shell-path-list-se.patch \
    0012-qt-5.8.0-Revert-fix-quoting-and-path-separators-in-qtPrepareT.patch \
    0013-qt-5.3.1-workaround-ansidecl-h-PTR-define-conflict.patch \
    0014-qt-5.3.2-dont-add-resource-files-to-qmake-libs.patch \
    0015-qt-5.3.2-win32-qt5-static-cmake-link-ws2_32-and--static.patch \
    0016-qt-5.4.2-win32-Avoid-platformNativeInterface-segfaults-with-minimal-platform.patch \
    0017-qt-5.8.0-win32-do-not-use-fontconfig.patch \
    0018-win32-dont-need-flatpack-theme.patch \
    0019-qt-5.11-static_icu.patch \
    0020-fix-and-enable-iconv-codec.patch \
    0021-enable-mingw-schannel-alpn.patch \
    0022-qt-5.8.0-cast-errors.patch \
    0024-fix-synqt-with-git-build.patch \
    0025-enable-avx-support.patch \
    0026-fix-mingw-win32-winnt-detection.patch

  # For: https://github.com/msys2/MINGW-packages/issues/10158
  # From: https://invent.kde.org/packaging/craft-blueprints-kde/-/blob/master/libs/qt5/qtbase/.qt-kde-5.15/qdbus-manager-quit-5.7.patch
  apply_patch_with_msg \
    0027-qt-5.7.0-qdbus-manager-quit.patch

  # https://lists.fedoraproject.org/archives/list/devel@lists.fedoraproject.org/thread/IJQ3KMKJFIKEVY4WX5WMQFYUGAVB2G2M/#TPQFP6W22XACTGIBLDL3TG6HA322HWPR
  apply_patch_with_msg \
    0028-work-around-pyside2-brokenness.patch

  local _ARCH_TUNE=
  case ${MINGW_CHOST} in
    i686*)
      _ARCH_TUNE="-march=pentium4 -mtune=generic"
    ;;
    x86_64*)
      _ARCH_TUNE="-march=nocona -msahf -mtune=generic"
    ;;
  esac

  BIGOBJ_FLAGS="-Wa,-mbig-obj"

  # Append these ones ..
  sed -i "s|^QMAKE_CFLAGS .*= \(.*\)$|QMAKE_CFLAGS            = \1 ${_ARCH_TUNE} ${BIGOBJ_FLAGS}|g" mkspecs/${_platform}/qmake.conf
  sed -i "s|^QMAKE_CXXFLAGS .*= \(.*\)$|QMAKE_CXXFLAGS            = \1 ${_ARCH_TUNE} ${BIGOBJ_FLAGS}|g" mkspecs/${_platform}/qmake.conf

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    sed -i "s| -mthreads||g" mkspecs/common/g++-win32.conf
  fi

  find . -name "*.orig" -exec rm -f {} \;
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  local -a _sql_config
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    _sql_config+=("-plugin-sql-ibase")
  fi
  _sql_config+=("-plugin-sql-psql")
  _sql_config+=("-plugin-sql-mysql")
  _sql_config+=("-plugin-sql-odbc")

  # Qt manages the compiler flags for release / debug configs separately, so having our own values (-O2) is harmful here ..
  unset CFLAGS
  unset CXXFLAGS
  unset LDFLAGS
  unset QMAKESPEC
  unset XQMAKESPEC
  unset QMAKEPATH
  unset QMAKEFEATURES

  export PATH="${srcdir}/build-${MSYSTEM}/bin:${srcdir}/build-${MSYSTEM}/lib:${PATH}"
  export VULKAN_SDK=${MINGW_PREFIX}

  ../${_pkgfn}/configure.bat \
    -prefix ${MINGW_PREFIX} \
    -datadir ${MINGW_PREFIX}/share/qt5 \
    -archdatadir ${MINGW_PREFIX}/share/qt5 \
    -nomake examples \
    -nomake tests \
    -opensource \
    -confirm-license \
    -platform ${_platform} \
    -shared \
    -make-tool make \
    -I${MINGW_PREFIX}/include/mariadb \
    -opengl desktop \
    -dbus \
    -fontconfig \
    -icu \
    -openssl \
    -system-doubleconversion \
    -system-freetype \
    -system-harfbuzz \
    -system-libjpeg \
    -system-libpng \
    -system-pcre \
    -system-sqlite \
    -system-zlib \
    -no-iconv \
    -release -force-debug-info -separate-debug-info \
    "${_sql_config[@]}"

  make
}

check() {
  cd build-${MSYSTEM}
  make check -j1 -k
}

package_qt5-base() {
  depends=("${MINGW_PACKAGE_PREFIX}-dbus"
           "${MINGW_PACKAGE_PREFIX}-double-conversion"
           "${MINGW_PACKAGE_PREFIX}-fontconfig"
           "${MINGW_PACKAGE_PREFIX}-freetype"
           "${MINGW_PACKAGE_PREFIX}-harfbuzz"
           "${MINGW_PACKAGE_PREFIX}-icu"
           "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
           "${MINGW_PACKAGE_PREFIX}-libpng"
           "${MINGW_PACKAGE_PREFIX}-md4c"
           "${MINGW_PACKAGE_PREFIX}-openssl"
           "${MINGW_PACKAGE_PREFIX}-pcre2"
           "${MINGW_PACKAGE_PREFIX}-sqlite3"
           "${MINGW_PACKAGE_PREFIX}-vulkan"
           "${MINGW_PACKAGE_PREFIX}-zlib"
           "${MINGW_PACKAGE_PREFIX}-zstd")
  optdepends=("${MINGW_PACKAGE_PREFIX}-libmariadbclient"
              "${MINGW_PACKAGE_PREFIX}-firebird2"
              "${MINGW_PACKAGE_PREFIX}-postgresql")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5")

  cd build-${MSYSTEM}

  # Fix Makefiles
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${srcdir}/build-${MSYSTEM}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|${PREFIX_WIN:0:2}\$(INSTALL_ROOT|\$(INSTALL_ROOT|g" {} \;
  find "${srcdir}/build-${MSYSTEM}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|)${PREFIX_WIN:2}|)${MINGW_PREFIX}|g" {} \;

  make INSTALL_ROOT="${pkgdir}" install

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 $srcdir/${_pkgfn}/LICENSE* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}

  # Seperate debug-info files
  mv -f "${pkgdir}${MINGW_PREFIX}/lib"/*.debug "${pkgdir}${MINGW_PREFIX}/bin"/ || true
  rsync -armR --remove-source-files --include="*/" --include="*.debug" --exclude="*" --prune-empty-dirs ${pkgdir}/.${MINGW_PREFIX} ${srcdir}/${MSYSTEM}-debug/

  # Fix paths in *.pri, *prl and *.pc files:
  #   $(cygpath -m ${MINGW_PREFIX}) -> $(cygpath -m ${pkgdir}${MINGW_PREFIX})
  local PKGDIR_PREFIX_WIN=$(cygpath -m ${pkgdir}${MINGW_PREFIX})
  local FAKE_PREFIX_FOR_REPLACE="@@QT_REAL_PREFIX/dir@@"

  find "${pkgdir}${MINGW_PREFIX}/lib" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/share/qt5" -type f -name '*.pri' \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PKGDIR_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
}

package_qt5-base-debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-base")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5-debug")
  options=('!strip')

  cp -rf ${srcdir}/${MSYSTEM}-debug${MINGW_PREFIX} "${pkgdir}"/
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
